<?php
try{
    $db = new PDO("mysql:host=localhost; dbname=db5;", 'root', '12qwas');
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
    session_start();
    $hasRight=$_SESSION['user']['xh']===$_GET['xh'] ||$_SESSION['user']['isAdmin'];//判断用户是否有修改的权限
    if($r=$_POST){
        if($r['pwd']!==$r['pwd2'])throw new Exception('两次输入的口令不一致，请重新输入~');
        if($r['pwd']){ //用户输入有新的口令，那就判断是否能满足规则要求，用正则表达式就比较方便
            if( preg_match('@^(?=.*\d)(?=.*?[a-z])(?=.*[^0-9a-z]).{3,20}$@',$r['pwd'])!==1){
                throw new Exception('口令需要3~20 个字符，须同时包含字母、数字及其他字符~');
            }
        }elseif ($_GET['xh']){//要修改用户信息，但是没有提供口令，表示用户原来的口令不用做修改
            $r['pwd']=$_SESSION['user']['pwd'];
        }else{//注册新用户却没有输入口令
            throw new Exception('注册用户必须提供口令~');
        }
        if($_GET['xh']){//修改已经存在的记录
            if ($hasRight){//有权限执行修改
                $ps=$db->prepare('update students set xh=?, name=?,tel=?,pwd=? where xh=?');
                $ps->execute([$r['xh'],$r['name'],$r['tel'],$r['pwd'],$_GET['xh']]);
                if($_SESSION['user']['xh']===$_GET['xh'])$_SESSION['user']=$r;//登录用户的信息被修改，这里也需要进行修改
            }else{//没有权限修改记录
                throw new Exception('Sorry,你没有操作的权限~');
            }
        }else{//注册新用户
            $ps=$db->prepare('insert into students (xh,name,tel,pwd) values (?,?,?,?)');
            $ps->execute([$r['xh'],$r['name'],$r['tel'],$r['pwd']]);
        }
        header('Location: index.php');
        return;
    }elseif($_GET['xh']){//修改用户信息
        $ps=$db->prepare( 'select * from students where xh=?');
        $ps->execute(array($_GET['xh']));
        $r=$ps->fetch();
        if($r===false)throw new Exception('找不到要修改的记录~');
        if (!$hasRight)throw new Exception('Sorry,你没有操作的权限~');
    }else{//注册新用户
        $hasRight=true;
    }
} catch(Throwable $e){
    $msg=$e->getMessage();
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title><?=$_GET['xh']?'修改':'注册'?>新用户</title>
    <style>
        h1{color: red;}
        input{padding: 10px; margin: 6px ;border-radius: 6px; }
        .msg{color: red;margin: 20px 0;}
    </style>
</head>
<body>


<h1><?=$_GET['xh']?'修改':'注册'?>新用户</h1>
<form method="post">
    学号:<input type="text" name="xh" value="<?=$r['xh']?>"<?= $hasRight?'':' disabled'?>><br>
    姓名:<input type="text" name="name" value="<?=$r['name']?>"<?= $hasRight?'':' disabled'?>><br>
    电话:<input type="text" name="tel" value="<?=$r['tel']?>"<?= $hasRight?'':' disabled'?>><br>
    口令:<input name="pwd" type="password"<?= $hasRight?"":" disabled" ?> /> <b><?=
        isset($_GET['xh'])&&!$msg?'留空将不会修改原来的口令~':'3~20 个字符，须同时包含字母、数字及其他字符' ?></b><br>
    再次:<input name="pwd2" type="password"<?= $hasRight?"":" disabled" ?> /><br>
    <div class="msg"><?=$msg?></div>
    <input type="submit" value="提交数据"<?= $hasRight?'':' disabled'?>>
</form>
</body>
</html>
